package com.njupt.towersutils.components;

import com.njupt.towersutils.domain.ManageInfos;
import com.njupt.towersutils.mapper.ManageInfosMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MySQLTORedis {
    @Resource
    private ManageInfosMapper manageInfosMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Scheduled(cron = "0 39 19 * * ?")
    public void managesDataTransform(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
        System.out.println("Start");
        System.out.println(sdf.format(new Date()));
        //读取所有的mysql managerinfos数据 发送到 Redis 数据库的manages hash中
        List<ManageInfos> mis = manageInfosMapper.findAll();
        for(ManageInfos mi:mis){
            stringRedisTemplate.opsForHash()
                    .put("manages",mi.getLoginname()+mi.getLoginpwd(),mi.toString());
        }
        System.out.println("Over");
        System.out.println(sdf.format(new Date()));
    }
}
